﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WanMeiCommon;
using WanMeiModel;
using WanMeiSqlManager;

namespace WanMeiBussinessService
{
    public class RoleService:IRoleService
    {

        #region IRoleService 成员

        public UserInfoModel GetUserInfoModel(string un, int heroId)
        {
            UserInfoModel result = null;
            SqlParameter[] parms = 
            { 
                new SqlParameter("@un",SecretClass.DecryptQueryString(un)),
                new SqlParameter("@heroId",heroId)
            };
            string sql = "select * from C_Role where HeroId=@heroId and UserName=@un";
            var ds = new SqlManager().ExecuteDataset(CommandType.Text,sql, parms);
            string heroName = ds.Tables[0].Rows[0]["HeroId"].ToString()=="1"?"剑士":"魔法师";
            if (ds != null && ds.Tables[0].Rows.Count == 1)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                result = new UserInfoModel(un,1,"",1);
                //result.RoleModel = new UserRoleModel();
                //result.RoleModel.Armor = Convert.ToInt32(dr["Armor"]);
                //result.RoleModel.CriticalValue = Convert.ToInt32(dr["CriticalValue"]);
                //result.RoleModel.DepositValue = Convert.ToInt32(dr["DepositValue"]);
                //result.RoleModel.ExValue = long.Parse(dr["ExValue"].ToString());
                //result.RoleModel.GameLevel = Convert.ToInt32(dr["GameLevel"]);
                //result.RoleModel.HealthPoint = Convert.ToInt32(dr["HealthPoint"]);
                //result.RoleModel.HeroId = Convert.ToInt32(dr["HeroId"]);
                //result.RoleModel.Id = Convert.ToInt32(dr["Id"]);
                //result.RoleModel.MagicPower = Convert.ToInt32(dr["MagicPower"]);
                //result.RoleModel.PotentialValue = long.Parse(dr["PotentialValue"].ToString());
                //result.RoleModel.RoleNick = dr["RoleNick"].ToString();
                //result.RoleModel.VipLevel = Convert.ToInt32(dr["VipLevel"]);
                //result.RoleModel.Money = long.Parse(dr["Money"].ToString());
                //result.RoleModel.ApValue = Convert.ToInt32(dr["ApValue"]);
                //result.RoleModel.LevelTitle = ToolManager.GetGameLevelTitle(heroName, Convert.ToInt32(dr["GameLevel"]));
            }
            return result;
        }

        /// <summary>
        /// 写入消息日志
        /// </summary>
        /// <param name="model"></param>
        public void WriteChatLog(ChatLogModel model)
        {
            string sql =string.Format("insert into C_ChatLog(Un,HeroId,ChatType,Nick,SendMsg)values('{0}',{1},{2},'{3}','{4}')",SecretClass.DecryptQueryString(model.Un),model.HeroId,model.ChatType,model.Nick,model.SendMsg);
            try
            {
                new SqlManager().ExecuteNonQuery(CommandType.Text, sql, null);
            }
            catch(Exception ex)
            {

                LogManger.Instance.WriteLog("写入消息日志出错:" + ex.ToString(), "RoleService");
            }
        }

        #endregion
    }
}
